//
//  AppDelegate.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "AppDelegate.h"
#import "GHTabbarViewController.h"
#import "GHLoginViewController.h"
#import <AlipaySDK/AlipaySDK.h>
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //最长2s
    [SVProgressHUD setMaximumDismissTimeInterval:2];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [GHTabbarViewController new];
    [self.window makeKeyAndVisible];
    if (![GHUserManger defult].token.length) {
        //无token 到登录界面
        self.window.rootViewController = [[STNavigationController alloc] initWithRootViewController:[GHLoginViewController new]];
    }

    return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            NSString * status = resultDic[@"resultStatus"];
            if ([status isEqualToString:@"9000"]) {
                [SVProgressHUD showSuccessWithStatus:[self hudFromCode:status]];
            }else{
                [SVProgressHUD showErrorWithStatus:[self hudFromCode:status]];
            }
        }];
    }
    return YES;
}
- (NSString*)hudFromCode:(NSString*)code{
    if ([code isEqualToString:@"9000"]) {
        return @"支付成功";
    }
    if ([code isEqualToString:@"8000"]) {
        return @"正在处理中";
    }
    if ([code isEqualToString:@"4000"]) {
        return @"订单支付失败";
    }
    if ([code isEqualToString:@"5000"]) {
        return @"重复请求，支付失败";
    }
    if ([code isEqualToString:@"6001"]) {
        return @"中途取消，支付失败";
    }
    if ([code isEqualToString:@"6002"]) {
        return @"网络连接错误";
    }
    if ([code isEqualToString:@"6004"]) {
        return @"支付结果未知";
    }
    return @"未知错误";
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
